{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**6. ZigZag Conversion**  <font color=#f0ad4e size=3>_medium_</font>\n",
    "\n",
    "The string \"PAYPALISHIRING\" is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)\n",
    "\n",
    "```\n",
    "P   A   H   N\n",
    "A P L S I I G\n",
    "Y   I   R\n",
    "```\n",
    "And then read line by line: ```PAHNAPLSIIGYIR```  \n",
    "Write the code that will take a string and make this conversion given a number of rows:  \n",
    "```string convert(string s, int numRows);```\n",
    "\n",
    "**Example 1**\n",
    "\n",
    "```\n",
    "Input: s = \"PAYPALISHIRING\", numRows = 3\n",
    "Output: \"PAHNAPLSIIGYIR\"\n",
    "```\n",
    "\n",
    "**Example 2**\n",
    "\n",
    "```\n",
    "Input: s = \"PAYPALISHIRING\", numRows = 4\n",
    "Output: \"PINALSIGYAHRPI\"\n",
    "Explanation:\n",
    "\n",
    "P     I    N\n",
    "A   L S  I G\n",
    "Y A   H R\n",
    "P     I\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "PAHNAPLSIIGYIR\n"
     ]
    }
   ],
   "source": [
    "def convert(s, numRows):\n",
    "    \"\"\"\n",
    "    P   A   H   N\n",
    "    A P L S I I G\n",
    "    Y   I   R\n",
    "    Input: s = \"PAYPALISHIRING\", numRows = 3\n",
    "    Output: \"PAHNAPLSIIGYIR\"\n",
    "    \"\"\"\n",
    "\n",
    "    first_line_index = []\n",
    "    output = []\n",
    "    for i in range(0, numRows):\n",
    "        if i == 0:\n",
    "            for i in range(0, len(s), 2 * numRows - 2):\n",
    "                first_line_index.append(i)\n",
    "                output.append(s[i])\n",
    "        elif i == numRows - 1:\n",
    "            for k in first_line_index:\n",
    "                if k+i < len(s):\n",
    "                    output.append(s[k+i])\n",
    "        else:\n",
    "            for j in first_line_index:\n",
    "                if j == 0 and j+i < len(s):\n",
    "                    output.append(s[j+i])\n",
    "                else:\n",
    "                    output.append(s[j-i])\n",
    "                    if j+i < len(s):\n",
    "                        output.append(s[j+i])\n",
    "\n",
    "    return \"\".join(output)\n",
    "\n",
    "\n",
    "if __name__ == \"__main__\":\n",
    "    print(convert(\"PAYPALISHIRING\", 3))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
